/** 
 * @(#)Packet.java
 */

/**
 * @author Robert Denda&Didier Gutacker
 * @version 1.1, 05.09.97
 *
 * original author Robert Denda, changes made by Didier Gutacker
 */

import java.awt.*;

public class Packet {
  private Color colour;
  public boolean forwardDirection;	// true means from node1 to node2
  private double currentTravelPercentage;
  private String contentName;
  private Object message;
  private int   pdiameter;
  private int timestamp;
  private int x, y;
  public Object toID; 
  private Lock resetCoordinatesGuard;

  public Packet( boolean forwardDirection, Color colour, String contentName, Object message,int pdiameter, int timestamp ) {
    this.forwardDirection = forwardDirection;
    this.colour = colour;
    this.message = message;
    this.pdiameter = pdiameter;
    this.contentName = contentName;
    this.timestamp = timestamp;
    currentTravelPercentage = 0.0;
    x = y = 0;
    resetCoordinatesGuard = new Lock();
  } // Packet.Packet

  /** 
   *  erweitertes Datenpaket fuer das Senden von einem bestimmten Rechner zu genau einem anderen
   *
   *  author Didier Gutacker 
   */  
 
   public Packet( boolean forwardDirection, Color colour, String contentName, Object message, int timestamp, Object toID ) {
    this.toID = toID;
    this.forwardDirection = forwardDirection;
    this.colour = colour;
    this.message = message;
    this.contentName = contentName;
    this.timestamp = timestamp;
    currentTravelPercentage = 0.0;
    x = y = 0;
    resetCoordinatesGuard = new Lock();
  } // Packet.Packet

  public void paint( Graphics gc, int x1, int y1, int x2, int y2 ) {
    if ( currentTravelPercentage != 0.0 ) {
      resetCoordinates( x1, y1, x2, y2 );
      Color oldColour = gc.getColor();
      gc.setColor( colour );
      resetCoordinatesGuard.acquire();
      gc.fillRect( x - 5, y - 5, 10, 10 );
      resetCoordinatesGuard.release();
      gc.setColor( oldColour );
    } // if
  } // Packet.paint

  public void unpaint( Graphics gc, int x1, int y1, int x2, int y2 ) {
    if ( currentTravelPercentage != 0.0 ) {
      resetCoordinatesGuard.acquire();
      gc.clearRect( x - 5, y - 5, 10, 10 );
      resetCoordinatesGuard.release();
    } // if
  } // Packet.paint

  void resetCoordinates( int x1, int y1, int x2, int y2 ) {
    double xmin = (double) Math.min( x1, x2 );
    double ymin = (double) Math.min( y1, y2 );
    double xmax = (double) Math.max( x1, x2 );
    double ymax = (double) Math.max( y1, y2 );
    double percentage = currentTravelPercentage / 100.0d;
    if ( ( forwardDirection && ( x1 > x2 ) ) || ( !forwardDirection && ( x2 > x1 ) ) ) {
      percentage = 1 - percentage;
    } // if
    resetCoordinatesGuard.acquire();
    x = (int) ( xmin + ( ( xmax - xmin ) * percentage ) );
    if ( ( xmin == x1 && ymin == y1 ) || ( xmin == x2 && ymin == y2 ) ) {
      y = (int) ( ymin + ( ( ymax - ymin ) * percentage ) );
    } else {
      y = (int) ( ymax - ( percentage * ( ymax - ymin ) ) ); 
    } // if 
    resetCoordinatesGuard.release();
  } // Packet.resetCoordinates


  synchronized public void increaseTravelPercentage( double value, int currentTimeStamp ) {
    if ( currentTimeStamp < timestamp ) {
      return;
    } // if
    if ( currentTravelPercentage + value > 100.0 ) {
      currentTravelPercentage = 100.0;
    } else {
      currentTravelPercentage += Math.max( value, 1 );
    } // if
  } // Packet.increaseTravelPercentage

  synchronized public double getTravelPercentage() {
    return currentTravelPercentage;
  } // Packet.getTravelPercentag

  public String getContentName() {
    return contentName;
  } // Packet.getContentName
    
  public Object getMessage() {
    return message;
  } // Packet.getMessage

  public Object gettoID(){
    return toID;
  } // Packet.gettoID
  public int getPDiameter(){
	  return pdiameter;
  }

} // Packet






